<?php
	/**
	* @File name:	loader.php
	* @Desription:	autoload
	* @Author:	 	jinxin<jx@178.com>   
	* @Date:	 	Mon Jul 25 10:49:28 CST 2011
	* @Copyright 	Copytright (c) 2008-2011 178.COM Co., Ltd.
	* @version	 	$Id $
	*/

	class loader {
		const coreIncludeFile = '/data_cache/coreInclude.php';
		public function __construct(){}
		public static function autoload($class){
			if (!is_file(DOCUMENT_PATH.self::coreIncludeFile)){
				self::getCoreLibList();
			}
			include_once(DOCUMENT_PATH.self::coreIncludeFile);
			if (class_exists($class,false) || interface_exists($class,false)){
				return ;
			}
			$coreArr = $_SERVER['config']['coreArr'];
			if (in_array($class.'.php',$coreArr)){
				include_once(LIB_PATH.$class.'.php');
			}else {
				$filePath = str_replace('_',DIRECTORY_SEPARATOR,$class).'.php';
				if (!is_file(SCRIPT_PATH.$filePath)){
					$msg = $_SERVER['config']['debug'] ? 'no_class' : 'no_uri';
					throw new Exception($msg);
				}
				require_once(SCRIPT_PATH.$filePath);
			}
		}
		
		private function getCoreLibList(){
			$file = scandir(LIB_PATH);
			$coreArr = array();
			foreach ($file as $value){
				if (substr($value,-4) == '.php'){
					$coreArr[] = $value;
				}
			}
			$txt = '<?php	
$_SERVER[\'config\'][\'coreArr\'] = '.var_export($coreArr,true).';

';
			file_put_contents(DOCUMENT_PATH.self::coreIncludeFile,$txt);
		}
	}
	
	